#include "weather.h"
#include "ui_weather.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTextCodec>
#include <QString>
#include <QDebug>

#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonDocument>
#include <QStringList>
#include <QPixmap>
#include <QJsonArray>

weather::weather(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::weather)
{
    ui->setupUi(this);

    netManager =new QNetworkAccessManager(this);

    QNetworkRequest ipRequest;

    ipRequest.setUrl(QUrl("http://whois.pconline.com.cn/ipJson.jsp?ip"));

    QNetworkReply * netReply=netManager->get(ipRequest);

    connect(netReply,SIGNAL(finished()),this,SLOT(slotGetReplyFinished()));

}

void weather::iconPath()
{
    iconList<<"晴"<<"多云"<<"阴"<<"雷雨"<<"雨夹雪"<<"小雨"<<"中雨"<<"大雨"<<"暴雨"<<"小雪"<<"中雪"<<"大雪";
}

weather::~weather()
{
    delete ui;
}

void weather::on_btn_back_clicked()
{
    this->close();
}

void weather::slotGetReplyFinished()
{
    QNetworkReply *reply=(QNetworkReply *)sender();

    QTextCodec *codec=QTextCodec::codecForName("gbk");
    QString data=codec->toUnicode(reply->readAll());

    reply->deleteLater();

    int cityLocation=data.indexOf("city")+7;
    int cityCodeLocation=data.indexOf("cityCode")-3;
    QString cityName=data.mid(cityLocation,cityCodeLocation-cityLocation);

    ui->location->setText(cityName);

    QNetworkRequest weatherRequest;

    weatherRequest.setUrl(QUrl("http://wthrcdn.etouch.cn/weather_mini?city="+cityName));

    QNetworkReply * weatherReply=netManager->get(weatherRequest);

    connect(weatherReply,SIGNAL(finished()),this,SLOT(slotGetWeatherReplyFinished()));
}

void weather::slotGetWeatherReplyFinished()
{
    QNetworkReply *reply=(QNetworkReply *)sender();

    QTextCodec *codec=QTextCodec::codecForName("utf8");
    QString data=codec->toUnicode(reply->readAll());

    reply->deleteLater();

    QJsonParseError jsonError;
    QJsonObject jsonObject;
    QJsonDocument jsonDocument;
    QString jsonData;
    QString iconPic; //图片路径

    iconPath();

    jsonDocument=QJsonDocument::fromJson(data.toUtf8(),&jsonError);

    if(!jsonDocument.isNull()&&(jsonError.error==QJsonParseError::NoError)){

        if(jsonDocument.isObject()){
            jsonObject=jsonDocument.object();
            jsonObject=jsonObject.value("data").toObject();

            //解析温度
            jsonData=jsonObject.value("wendu").toString();
            ui->temp->setText(jsonData);

            //解析感冒
            jsonData=jsonObject.value("ganmao").toString();
            ui->text->setText(jsonData);

            jsonObject=jsonObject.value("yesterday").toObject();

            jsonData=jsonObject.value("type").toString();
            ui->weather4->setText(jsonData);

            for(int i=0;i<12;i++){
                if(jsonData==iconList[i]){
                    iconPic=":/weather_res/"+QString::number(i+1)+".png";
                    break;
                }
            }

            QPixmap iconPix(iconPic);
            iconPix=iconPix.scaled(ui->weather1->width(),ui->weather1->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

            ui->weather1->setPixmap(iconPix);

            jsonData=jsonObject.value("low").toString().mid(3,2)+"~";
            jsonData=jsonData+jsonObject.value("high").toString().mid(3,2);
            ui->temp1->setText(jsonData);
        }

        /*今天天气*/
        jsonObject=jsonDocument.object();
        jsonObject=jsonObject.value("data").toObject();

        QJsonArray forecast=jsonObject.value("forecast").toArray();
        jsonObject=forecast.at(0).toObject();

        jsonData=jsonObject.value("low").toString().mid(3,2)+"~";
        jsonData=jsonData+jsonObject.value("high").toString().mid(3,2);
        ui->temp2->setText(jsonData);

        jsonData=jsonObject.value("type").toString();
        ui->weather5->setText(jsonData);

        for(int i=0;i<12;i++){
            if(jsonData==iconList[i]){
                iconPic=":/weather_res/"+QString::number(i+1)+".png";
                break;
            }
        }

        QPixmap iconPix1(iconPic);
        iconPix1=iconPix1.scaled(ui->weather2->width(),ui->weather2->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
        ui->weather2->setPixmap(iconPix1);

        /*明天天气*/
        jsonObject=forecast.at(1).toObject();

        jsonData=jsonObject.value("low").toString().mid(3,2)+"~";
        jsonData=jsonData+jsonObject.value("high").toString().mid(3,2);
        ui->temp3->setText(jsonData);

        jsonData=jsonObject.value("type").toString();
        ui->weather6->setText(jsonData);

        for(int i=0;i<12;i++){
            if(jsonData==iconList[i]){
                iconPic=":/weather_res/"+QString::number(i+1)+".png";
                break;
            }
        }

        QPixmap iconPix2(iconPic);
        iconPix2=iconPix2.scaled(ui->weather3->width(),ui->weather3->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
        ui->weather3->setPixmap(iconPix2);

    }
}
